﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;


namespace MissedMemo.MeatAndCheeseDisc.OrderEntryDataAccess
{
    public static class LookupMenuItem
    {
        public readonly static string strMenuFileName = "Menu.xml";


        public static IEnumerable<string> GetTypes( string strItemCategory )
        {
            XElement data = XElement.Load( strMenuFileName );

            return
                from itemType in data.Descendants( strItemCategory )
                select (String)itemType.Attribute( "DisplayName" );
        }


        public static IEnumerable<string> GetSizes( string strItemCategory, string strItemName )
        {
            XElement data = XElement.Load( strMenuFileName );

            return
                from itemType in data.Descendants( strItemCategory )
                where (String)itemType.Attribute( "DisplayName" ) == strItemName
                from itemSize in itemType.Descendants( "Size" )
                select (String)itemSize.Attribute( "DisplayName" );
        }


        public static double GetPrice( string strItemName, string strItemSize )
        {
            XElement data = XElement.Load( strMenuFileName );

            var query =
                from itemType in data.Descendants()
                where (String)itemType.Attribute( "DisplayName" ) == strItemName
                from itemSize in itemType.Descendants( "Size" )
                where (String)itemSize.Attribute( "DisplayName" ) == strItemSize
                select itemSize.Descendants( "Price" ).FirstOrDefault();

            double price = 0;

            if( query.ElementAt( 0 ) != null )
                Double.TryParse( query.ElementAt( 0 ).Value, out price );

            return price;
        }


        public static IEnumerable<string> GetOptionsAndToppings( string strPizzaType, string strPizzaSize )
        {
            XElement data = XElement.Load( strMenuFileName );

            IEnumerable<string> toppings = new List<string>();

            if( strPizzaType == "Custom" )
            {
                toppings =
                    from item in data.Descendants( "Topping" )
                    select (String)item.Attribute( "DisplayName" );
            }

            IEnumerable<string> options =
                from option in data.Descendants( "PizzaOption" )
                from size in option.Descendants( "Size" )
                where (String)size.Attribute( "DisplayName" ) == strPizzaSize
                select (String)option.Attribute( "DisplayName" );

            return options.Concat( toppings );
        }


        public static double GetToppingOrOptionPrice( string strItem, string strPizzaSize )
        {
            double price = GetOptionPrice( strItem, strPizzaSize );

            if( price == 0 )
                price = GetToppingPrice( strItem, strPizzaSize );

            return price;
        }


        private static double GetOptionPrice( string strOption, string strPizzaSize )
        {
            XElement data = XElement.Load( strMenuFileName );

            var query =
                from option in data.Descendants( "PizzaOption" )
                where (String)option.Attribute( "DisplayName" ) == strOption
                from size in option.Descendants( "Size" )
                where (String)size.Attribute( "DisplayName" ) == strPizzaSize
                select size.Element( "Price" ).Value;

            double price = 0;

            if( query.Count() > 0 )
                Double.TryParse( query.ElementAt( 0 ), out price );

            return price;
        }


        public static double GetToppingPrice( string strTopping, string strPizzaSize )
        {
            XElement data = XElement.Load( strMenuFileName );

            var query =
                from tier in data.Descendants( "PricingTier" )
                from topping in tier.Descendants( "Topping" )
                where (String)topping.Attribute( "DisplayName" ) == strTopping
                from size in tier.Descendants( "Size" )
                where (String)size.Attribute( "DisplayName" ) == strPizzaSize
                select size.Element( "Price" ).Value;

            double price = 0;

            if( query.Count() > 0 )
                Double.TryParse( query.ElementAt( 0 ), out price );

            return price;
        }
    }
}
